package com.ai.sml.util;



import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * 渠道用户对象工具类
 * @author lim
 *
 */
public class MerchantInfoUtils {
	private static final Logger logger = LoggerFactory.getActionLog(MerchantInfoUtils.class);
	 /**
     * 获取关系人ID
     * @author 
     */
	public static String getPtyId() {
		return getStrByKey("ptyId");
	}
	/**
     * 获取关系人角色类型代码
     * @author 
     */
	public static String getPtyRoleTypeCd() {
		return getStrByKey("ptyRoleTypeCd");
	}
	 /**
     * 获取用户ID
     * @author 
     */
	public static String getUserId() {
		return getStrByKey("userId");
	}
	 /**
     * 获取用户登录ID
     * @author 
     */
	public static String getUserLoginId() {
		return getStrByKey("userLoginId");
	}
	 /**
     * 获取用户登录ID类型代码
     * @author 
     */
	public static String getUserLoginIdTypeCd() {
		return getStrByKey("userLoginIdTypeCd");
	}
	/**
     * 获取用户别名
     * @author 
     */
	public static String getUserAls() {
		return getStrByKey("userAls");
	}
	 /**
     * 获取渠道ID
     * @author 
     */
	public static String getChnlId() {
		return getStrByKey("chnlId");
	}
	 /**
     * 获取首次登录标志
     * @author 
     */
	public static String getFtimeLoginFlag() {
		return getStrByKey("ftimeLoginFlag");
	}
	 /**
     * 获取有效状态代码
     * @author 
     */
	public static String getValidStsCd() {
		return getStrByKey("validStsCd");
	}
	 /**
     * 获取验证标志
     * @author 
     */
	public static String getVldFlag() {
		return getStrByKey("vldFlag");
	}
	 /**
     * 获取匹配用户ID
     * @author 
     */
	public static String getMatchUserId() {
		return getStrByKey("matchUserId");
	}
	
	
	
	/**
	 * 通用工具类 根据 key值获取merchantInfo对象中的value
	 * @param key
	 * @return
	 */
	public static String getStrByKey(String key) {
		try {
			Session session = SecurityUtils.getSubject().getSession();
			if(session!=null){
				JSONObject jsonObj = JSONObject.fromObject(session.getAttribute("merchantInfo"));
				if(session.getAttribute("merchantInfo")!=null){
					return jsonObj.getString(key);
				}
			}
		}catch (Exception e) {logger.error("getStrByKey","getStrByKey",e);
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 从session中获取openId
	 * @return
	 */

	public static String getOpenidFromSession() {
		String openid = "1";
		try {
			Session session = SecurityUtils.getSubject().getSession();
			if(session!=null){
				openid = null == session.getAttribute("openid") ? "1"
						: (String) session.getAttribute("openid");
			}
		}catch (Exception e) {logger.error("getOpenidFromSession","getOpenidFromSession",e);
			e.printStackTrace();
		}
		return openid;
	}
	
	/**
	 * 根据客户端请求获取ip地址
	 * @param request
	 * @return ip地址
	 * @throws Exception
	 */
	public static String getIpAddress(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }
		return ip;
	}
}
